package com.hou.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hou.mapper.UserMapper;
import com.hou.pojo.Result;
import com.hou.pojo.User;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.plaf.basic.BasicScrollPaneUI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-09-15-21:47
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    // 判断用户名密码是否匹配
    @Override
    public Result nameMatchPassword(String name, String password) {
        Result result = new Result();
        try {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_name",name);
            User user = userMapper.selectOne(queryWrapper);
            if (user.getUserPassword().equals(password)) {
                result.setUserId(user.getId()).setMsg("登录成功");
                return result;
            } else {
                result.setRESULT(false).setMsg("密码错误");
                return result;
            }
        } catch (Exception e){
            e.printStackTrace();
            result.setRESULT(false).setMsg("账号或密码错误");
            return result;
        }
    }

    // 用户等级
    @Override
    public String userStatus(Integer id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        User user = userMapper.selectOne(queryWrapper);
        return user.getUserStatus();
    }

    // 添加用户
    @Override
    public Boolean addUser(User user) {
        try {
            userMapper.insert(user);
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    // 删除用户
    @Override
    public Boolean deleteUser(Integer id) {
        try {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",id);
            userMapper.delete(queryWrapper);
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    // 更新用户
    @Override
    public Boolean updateUser(User user) {
        try {
            userMapper.updateById(user);
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    //显示所有用户
    @Override
    public List selectAllUser() {
        List<User> userList = userMapper.selectList(null);
        return userList;
    }

    // 查询当前用户
    @Override
    public User getNowUser(Integer id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }
}
